from typing import List

from sqlalchemy import exc, or_, func, and_
from sqlalchemy.orm import Session, with_loader_criteria, joinedload

from model.ParamModel import SysDictModel, SysDictDetailModel, UserModel, PortMenuModel, MenuModel
from schema.ParamSchema import SysDictInfo, SysDictDetailInfo, UserInfo, UserModPwdInfo
from util.ErrorCode import ErrorCode
from util.LogHelper import Log


def query_user_by_userid(user_id: str, db: Session):
    try:
        result = db.query(UserModel).filter(UserModel.user_id == user_id).one_or_none()
        return {'code': ErrorCode.SUCCESS.value, 'message': 'success', 'data': result}
    except exc.SQLAlchemyError as e:
        Log.getlogger().error(f'query_user_by_userid failed! user_id: {user_id} message: {e}')
        return {'code': ErrorCode.DATABASE_ERROR.value, 'message': 'database error'}


def mod_password(user_id: str, userModPwdInfo: UserModPwdInfo, db: Session):
    userModel = db.query(UserModel).filter(UserModel.user_id == user_id).one_or_none()
    if userModel is not None:
        if userModPwdInfo.new_password is not None:
            userModel.password = userModPwdInfo.new_password
        try:
            db.commit()
            return {'code': ErrorCode.SUCCESS.value, 'message': 'success'}
        except exc.SQLAlchemyError as e:
            Log.getlogger().error(f'mod_password failed! user_id:{user_id} message: {e}')
            return {'code': ErrorCode.DATABASE_ERROR.value, 'message': 'database error'}
    else:
        Log.getlogger().error(f'user_id:{user_id} is not exists!')
        return {'code': ErrorCode.DATABASE_RECORD_NOT_FOUND.value, 'message': 'record not exists!'}


def add_user(userInfo: UserInfo, db: Session):
    userModel = UserModel(user_id=userInfo.user_id, port_id=userInfo.port_id, user_name=userInfo.user_name,
                          password=userInfo.password,
                          status=1)
    try:
        db.add(userModel)
        db.commit()
        return {'code': ErrorCode.SUCCESS.value, 'message': 'success'}
    except exc.SQLAlchemyError as e:
        db.rollback()
        Log.getlogger().error(f'add_user failed:{userInfo} message: {e}')
        return {'code': ErrorCode.DATABASE_ERROR.value, 'message': 'database error'}


def mod_user(userInfo: UserInfo, db: Session):
    userModel = db.query(UserModel).filter(UserModel.user_id == userInfo.user_id).one_or_none()
    if userModel is not None:
        if userInfo.user_name is not None:
            userModel.user_name = userInfo.user_name if len(
                userInfo.user_name) != 0 else userModel.user_name
        if userInfo.port_id != 0:
            userModel.port_id = userInfo.port_id
        if userInfo.status != 0:
            userModel.status = userInfo.status

        try:
            db.commit()
            return {'code': ErrorCode.SUCCESS.value, 'message': 'success'}
        except exc.SQLAlchemyError as e:
            Log.getlogger().error(f'mod_user failed:{userInfo} message: {e}')
            return {'code': ErrorCode.DATABASE_ERROR.value, 'message': 'database error'}
    else:
        Log.getlogger().error(f'user_id:{userInfo.user_id} is not exists!')
        return {'code': ErrorCode.DATABASE_RECORD_NOT_FOUND.value, 'message': 'record not exists!'}
